<?php
class Action
{

	function index(){
			if(!$p = _GET('q', 'admin')){

				msg('Q is not empty');

			}
			$info=_model('admin_resource')->read(array('title'=>$p));

			$model_info=_model('admin_attribute_value')->read(array('res_id'=>$info['id']));

			if($model_info['id']){

				$model_info['id']=15;
			}

		$model = _model('admin_attribute')->getList('where model_id = '.$model_info['id'].' order by view_order desc');
		
		for($i=0,$b=count($model);$i<=$b;$i++){
			
			$x[]=$model[$i]['type'];
			
		}
		
		$a=null;
		
		if(in_array('file', $x)){
			
			$a.="<form action='?url=text_model/create' method='post' enctype='multipart/form-data'>";
			
		}else{
			
			$a.= "<form action='?url=text_model/create' method='post' >";
		}
		
		
		foreach($model as $val){
			
			if($val['type'] == 'select'){
				$val['value']=nl2br($val['value']);
				
				$new = explode("\n",$val['value']);
				
				$a.="<select name=attribute[".$val['id']."]>";
				
				foreach($new as $newval){
					$a.= "<option value =".$newval."{$newval}</option>";
					
				}
				$a.="</select><br>";
			}
			if($val['type'] == 'input'){
				
				$a.= "<input type='text' name=attribute[".$val['id']."]value ={$val['value']}><br>";
				
			}
			if($val['type'] == 'text'){
				
				$a.="<textarea name=arrtribute[".$val['id']."]>{$val['value']}</textarea><br>";
				
			}
			if($val['type'] == 'file'){
				
				$a.= "<input type='hidden' name='MAX_FILE_SIZE' value=30000 ><input name=attribute[".$val['id']."]type='file' />";
				
			}
			
		}
		$a.=" <input type='submit' value='Submit' />";
		$a.="</form>";
		echo $a;
	}

	   function create()
	   {
	       if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	          $attribute = _POST('attribute', array());
	          $add_time  = time();
	          $title     = _POST('title', '');
	          $mid       = _POST('mid', 0);
	          //$t_attribute = _model(_t('attribute'))->getList(array('model_id'=>$mid), ' ORDER BY view_order ASC ')
	          $res_id = _model('admin_resource')->create(array('title'=>$title, 'model_id'=>$mid, 'add_time'=>$add_time));
	echo $res_id;
	          if (!$res_id) {
	            msg('添加失败');
	          }
	            if ($_FILE) {
	                if (isset($_FILES["attribute_".$value['id']]) && is_uploaded_file($_FILES["attribute_".$value['id']]["tmp_name"])) {
	                    $attribute[$value['id']] = Storage::add($_FILES["attribute_".$value['id']]["tmp_name"], $_FILES["attribute_".$value['id']]["name"]);
	                }
	            }
	          foreach ($attribute as $key => $value) {
	              if (!empty($value)) {
	                  $a=_model('admin_attribute_value')->create(array('res_id'=>$res_id, 'attribute_id'=>$key, 'value'=>$value));
	              }
	          }
	          //msg('添加成功','?url=resource/create&mid='.$mid);
	       }
	   }

	   function update()
	   {
	        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	            $res_id =  _POST('rid', 0);
	            $attribute = _POST('attribute', array());
	            $add_time  = time();
	            $title     = _POST('title', '');
	            $mid       = _POST('mid', 0);
	            $t_attribute = _model('admin_attribute')->getList(array('model_id'=>$mid), ' ORDER BY view_order ASC ');
	            _model('admin_resource')->update(array('id'=>$res_id), array('title'=>$title));
	                if ($_FILE) {
	                    if (isset($_FILES["attribute_".$value['id']]) && is_uploaded_file($_FILES["attribute_".$value['id']]["tmp_name"])) {
	                        $attribute[$value['id']] = Storage::add($_FILES["attribute_".$value['id']]["tmp_name"], $_FILES["attribute_".$value['id']]["name"]);
	                    }
	                }
	            foreach ($attribute as $key => $value) {
	                 if (!$r = _model('admin_attribute_value')->read(array('res_id'=>$res_id, 'attribute_id'=>$key))) {
	                     _model('admin_attribute_value')->create(array('res_id'=>$res_id, 'attribute_id'=>$key, 'value'=>$value));
	                 } else {
	                     _model('admin_attribute_value')->update(array('res_id'=>$res_id, 'attribute_id'=>$key), array('value'=>$value));
	                 }
	            }
	          //  msg('更新成功','?url=resource&mid='.$mid);
	        } else {
	           $res_id = _GET('id', 0);
	           if ($res_id && $res_info = _model('admin_resource')->read(array('id'=>$res_id))) {
					$res_info['model_id']=15;
	               $attribute = _model('admin_attribute')->getList(array('model_id'=>$res_info['model_id']), ' ORDER BY view_order ASC ');
	               foreach ($attribute as $key => $value) {
	                    if ($value['type'] == 'select') {
	                       $attribute[$key]['value'] = array_map('trim', explode("\n", $value['value']));
	                    }
	               }
	var_dump($attribute);
	exit();
	               Smarty3::instance()->assign('res_info', $res_info);
	               Smarty3::instance()->assign('attribute', $attribute);
	               Smarty3::instance()->assign('mid', $res_info['model_id']);
	               Smarty3::instance()->display('text_model_update.html');
	           } else {
	               // msg('参数有误或不存在这个内容');
	           }
	        }
	   }

	   function delete()
	   {
	        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	            $id_array = array_filter(_POST('check', array()));
	            $rid = end($id_array);
	            $res_info = _model('admin_resource')->read(array('id'=>$rid));
	            foreach ($id_array as $key => $value) {
	                _model('admin_resource')->delete(array('id'=>$value));
	                _model('admin_attribute_value')->delete(array('res_id'=>$value));
	            }
	            redirect('?url=resource&mid='.$res_info['model_id']);
	        } else {
	            $id = _GET('id', 0);
	            if ($id && $res_info = _model('admin_resource')->read(array('id'=>$id))) {
	                _model('admin_resource')->delete(array('id'=>$id));
	                _model('admin_attribute_value')->delete(array('res_id'=>$id));
	                redirect('?url=resource&mid='.$res_info['model_id']);
	            }
	        }
	   }

	   function del_att_value()
	   {
	       $res_id = _GET('res_id', 0);
	       $attribute_id = _GET('attribute_id', 0);
	       _model('admin_attribute_value')->update(array('res_id'=>$res_id, 'attribute_id'=>$attribute_id), array('value'=>''));
	       echo '删除成功';
	   }
	function find($p){


		if(!$p = _GET('q', 'admin')){

			msg('Q is not empty');

		}
		$info=_model('admin_resource')->read(array('title'=>$p));

		$model_info=_model('admin_attribute_value')->read(array('res_id'=>$info['id']));

		if($model_info['id']){

			$model_info['id']=15;
		}		
			return $model_info['id']=15;
	}

}
	